package com.changgou.util;

import com.changgou.file.FastDFSFile;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @auther LiZiBa
 * @date 2020/7/26 15:32
 * @description: 文件上传、下载、删除、信息获取、Storage信息获取、Tracker信息获取
 **/
public class FastDFSUtil {


    /**
     * 加载Tracker 信息
     */
    static{

        try {
            // 查找classpath下的文件路径
            String filename = new ClassPathResource("fdfs_client.conf").getPath();
            // 初始化tracker的连接信息
            ClientGlobal.init(filename);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }

    }

    /**
     * 文件上传
     * @param fastDFSFile
     * @return
     */
    public static String[] upload(FastDFSFile fastDFSFile) throws Exception{

        // 通过storageCilent访问Storage，实现文件上传，并且获取文件上传后的存储信息
        
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] = new NameValuePair("author", "LiZiBa");
        /**
         * @param byte[] file_buff, 文件字节内容
         * @param String file_ext_name, 文件扩展名
         * @param org.csource.common.NameValuePair[] meta_lis 文件其他备注信息
         * @return  String[] uploads
         *          uploads[0] == storage group  名称
         *          uploads[1] == storage group  文件访问地址
         */
        String[] uploads = crateStorageClient() .upload_file(fastDFSFile.getContent(), fastDFSFile.getExt(), meta_list);

        return uploads;
    }

    /**
     * 获取文件
     * @param  group_name          组名 group1
     * @param  remote_filename     M00/00/00/wKjThF8dR06AVX7dAALp18zUzxo137.jpg
     * @throw  java.io.IOException
     * @return FileInfo
     */
    public static FileInfo getFile(String group_name, String remote_filename) throws Exception{

        // 通过StorageClient获取文件
        FileInfo file_info = crateStorageClient() .get_file_info(group_name, remote_filename);

        return file_info;
    }


    /**
     * 获取文件
     * @param  group_name          组名 group1
     * @param  remote_filename     M00/00/00/wKjThF8dR06AVX7dAALp18zUzxo137.jpg
     * @throw  java.io.IOException
     * @return InputStream
     */
    public static InputStream downloadFile(String group_name, String remote_filename) throws Exception{

        // 下载文件
        byte[] download_file_bytes = crateStorageClient() .download_file(group_name, remote_filename);

        return new ByteArrayInputStream(download_file_bytes);
    }


    /**
     * 删除文件
     * @param group_name         组名 group1
     * @param remote_filename    M00/00/00/wKjThF8dR06AVX7dAALp18zUzxo137.jpg
     * @throws Exception
     */
    public static void deleteFile(String group_name, String remote_filename) throws Exception{

        // 删除文件
        int i = crateStorageClient() .delete_file(group_name, remote_filename);
    }


    /**
     * 获取StorageCilent对象存储Storage的连接信息
     * @return StorageClient
     * @throws Exception
     */
    private static StorageClient crateStorageClient() throws Exception{

        // 创建一个Tracker访问的客户端对象TrackerClient对象
        TrackerClient trackerClient = new TrackerClient();

        // 通过TrackerCilent访问TrackerServer
        TrackerServer trackerServer = trackerClient.getConnection();

        // 通过trackerServer获取StorageServer连接信息，创建StorageCilent对象存储Storage的连接信息
        StorageClient storageClient = new StorageClient(trackerServer, null);

        return storageClient;
    }



}
